//
//  SEGEditDetailViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/10.
//

#import "SEGEditDetailViewModel.h"

@implementation SEGEditDetailViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createJobListCommand];
        [self seg_createUpdateInfoCommand];
    }
    return self;
}
- (void)seg_createJobListCommand {
    @weakify(self);
    self.jobListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.myManager requestUserJobMessageFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    self.dataSourceArr = self.myManager.seg_segjobmessagemodel;
                    SEGProfileModel *account = [SEGProfileManager sharedManager].profile;
                    if (![account.job isEqualToString:@"0"]) { // 有职业字段时去遍历 job接口默认传字符串"0"
                        // 匹配接口获得的jobid
                        [self.dataSourceArr enumerateObjectsUsingBlock:^(SEGJobMessageModel *model, NSUInteger idx_section, BOOL * _Nonnull stop) {
                            NSArray *childArray = model.childList;
                            [childArray enumerateObjectsUsingBlock:^(SEGJobNodeMessageModel *nodelModel, NSUInteger idx_cell, BOOL * _Nonnull stop) {
                                if ([account.job isEqualToString:nodelModel.professionId]) {
                                    [self.sectionArray addObject:[NSString stringWithFormat:@"%zd",idx_section]];
                                }
                            }];
                            
                        }];
                    }
                    [subscriber sendNext:@""];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}
- (void)seg_createUpdateInfoCommand {
    @weakify(self);
    self.updateInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *recordProfessionId) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *path = [doc stringByAppendingPathComponent:@"updateApartInfo.plist"];
            // 读取数据
            NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
            NSString *sex = [dict objectForKey:@"sex"];
            NSString *nickName = [dict objectForKey:@"nickName"];
            NSString *imagePath = [dict objectForKey:@"imagePath"];
            [self.myManager requestUpdateProperyInfoBynickname:nickName ico:imagePath name:@"" age:@"" sex:sex job:recordProfessionId desc:nil identity:@"" Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                [subscriber sendNext:errorMessage];
                if (isSuccessed) {
                    [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                        if (isSuccessed) {
                            [subscriber sendNext:@""];
                            [subscriber sendCompleted];
                        } else {
                            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                            [subscriber sendError:error];
                        }
                    }];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}

#pragma mark - Getter
- (NSMutableArray *)sectionArray {
    if (!_sectionArray) _sectionArray = [[NSMutableArray alloc] init];
    return _sectionArray;
}
- (SEGMyManager *)myManager {
    if (!_myManager) _myManager = [[SEGMyManager alloc] init];
    return _myManager;
}

@end
